using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using Avalonia.Controls.Notifications;
using AvaloniaApplication1.Models;
using AvaloniaApplication1.Views;
using CommunityToolkit.Mvvm.Input;
using SukiUI.Controls;
using SukiUI.Dialogs;

namespace AvaloniaApplication1.ViewModels.CardViewModel;

public partial class DeviceCardViewModel:INotifyPropertyChanged
{
    public ISukiDialogManager DialogManager { get; }
   public DeviceCardModel _deviceCardModel { get; }
   public DialogTestViewModel DialogTestVm { get; }
   public int? StartAddress { get; set; }

    public event PropertyChangedEventHandler? PropertyChanged;

    protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    protected bool SetField<T>(ref T field, T value, [CallerMemberName] string? propertyName = null)
    {
        if (EqualityComparer<T>.Default.Equals(field, value)) return false;
        field = value;
        OnPropertyChanged(propertyName);
        return true;
    }

    public DeviceCardViewModel(DeviceCardModel deviceCardModel, ISukiDialogManager dialogManager,DialogTestViewModel DialogTest)
    {
        _deviceCardModel = deviceCardModel;
        DialogManager = dialogManager;
        DialogTestVm=DialogTest;
        if (deviceCardModel.ModbusParams.TryGetValue("StartAddress", out var value) &&
            int.TryParse(value?.ToString(), out int startAddress))

        {
            StartAddress = (int)startAddress;
        }

        ;
    }

    [RelayCommand]
    private void OptionClicked(bool withIcon)
    {
        Console.WriteLine("Clicked Context option Menu");
        // 弹窗前先关闭弹窗
        DialogManager.DismissDialog();
        DialogManager.CreateDialog()
            .WithTitle($"{_deviceCardModel.DeviceName}")
            .WithViewModel(d=>DialogTestVm)
            .Dismiss().ByClickingBackground()
            .ShowCardBackground(false)
          //  .OfType(NotificationType.Information)
            .TryShow();
    }

    [RelayCommand]
    private void NestedOptionClicked()
    {
        Console.WriteLine("Clicked Context Nest");

   
    }
}